<?php
namespace app\admin\model;

use think\Model;

class ProductSku extends Base
{
    // 设置当前模型的数据库连接
    public function product(){
        return $this->hasOne("Product","id","product_id")->bind("product_code,cn_name,is_publish,cover_img");
    }
    public function ListHasWhere($with, $where, $where2,$field = '*', $order = '', $per_page_number = 15, $is_uid = 0)
    {
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $data = array();
        if (isset($where['page']) && $where['page'] != "") {
            $data['page'] = $where['page'];
            unset($where['page']);
        } else {
            $data['page'] = 1;
        }
        if (isset($where['submit_time']) && $where['submit_time'] != "") {
            $data['submit_time'] = $where['submit_time'];
            unset($where['submit_time']);
        }
        if (!isset($data['submit_time'])) {
            $dataList = $this->hasWhere($with,$where2)->where($where)->order($order)->paginate($per_page_number, false, [
                'page' => $data['page']])->each(function ($item,$key){
                $item['create_time'] = date("Y-m-d H:i",$item['create_time']);
                return $item;
            });
        }
        foreach ($dataList as $key=>$value){
            $dataList[$key]['cn_name'] = $value['product']['cn_name'];
            $dataList[$key]['product_code'] = $value['product']['product_code'];
            $dataList[$key]['product_img'] = $value['product']['cover_img'];
            if($value['product']['is_publish'] ==1){
                $dataList[$key]['status'] = "下架";
            }else{
                $dataList[$key]['status'] = "上架";
            }
        }
        $dataList=$dataList->toArray();
        return $dataList['data'];
    }
}